/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xuidemo.slice.refresh;

import com.isotne.glidelibrary.utils.OhosGlide;
import com.xuexiang.xui_lib.adapter.recyclerview.BaseRecyclerAdapter;
import com.xuexiang.xui_lib.adapter.recyclerview.RecyclerViewHolder;
import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.item.NewInfo;
import com.xuexiang.xuidemo.item.StickyItem;

import java.util.List;

/**
 * 粘性头部适配器
 *
 * @since 2021-04-22
 */
public class StickyListProvider extends BaseRecyclerAdapter<StickyItem> {
    /**
     * 头部标题
     */
    public static final int TYPE_HEAD_STICKY = 1;
    /**
     * 新闻信息
     */
    private static final int TYPE_NEW_INFO = 2;

    /**
     * 构造方法
     *
     * @param data 默认数据集
     */
    public StickyListProvider(List<StickyItem> data) {
        super(data);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    protected void bindData(RecyclerViewHolder holder, int position, StickyItem item) {
        if (getItemComponentType(position) == TYPE_HEAD_STICKY) {
            holder.text(ResourceTable.Id_tv_title, item.getHeadTitle());
        } else {
            NewInfo info = item.getNewInfo();
            holder.text(ResourceTable.Id_tv_user_name, info.getUserName());
            holder.text(ResourceTable.Id_tv_tag, info.getTag());
            holder.text(ResourceTable.Id_tv_title, info.getTitle());
            holder.text(ResourceTable.Id_tv_summary, info.getSummary());
            holder.text(ResourceTable.Id_tv_praise, info.getPraise() == 0 ? "点赞" : String.valueOf(info.getPraise()));
            holder.text(ResourceTable.Id_tv_comment, info.getComment() == 0 ? "评论" : String.valueOf(info.getComment()));
            holder.text(ResourceTable.Id_tv_read, "阅读量 " + info.getRead());
            OhosGlide.with(holder.getItemView().getContext())
                .load(info.getImageUrl())
                .def(ResourceTable.Media_xui_ic_default_img)
                .into(holder.getImageView(ResourceTable.Id_iv_image));
        }
    }

    @Override
    public int getItemComponentType(int position) {
        if (getItem(position).isHeadSticky()) {
            return TYPE_HEAD_STICKY;
        } else {
            return TYPE_NEW_INFO;
        }
    }

    @Override
    public int getComponentTypeCount() {
        final int typeCount = 2;
        return typeCount;
    }

    @Override
    protected int getItemLayoutId(int viewType) {
        if (viewType == TYPE_HEAD_STICKY) {
            return ResourceTable.Layout_adapter_sticky_title_item;
        } else {
            return ResourceTable.Layout_adapter_news_xuilayout_list_item;
        }
    }
}
